import http from "@/api/http";
import router from "../router";

export async function getPlugins() {
    try {
      const result = await http
        .get(`/editor/plugin`)
        .then(function (response) {
          return response.data.plugins;
        });
      return result;
    } catch (error) {}
  };

export async function searchPlugins(keywords) {
    try {
      const result = await http
        .post(`/editor/plugin/search`,{keyword: keywords})
        .then(function (response) {
          return response.data.plugins;
        });
      return result;
    } catch (error) {}
};

export async function getPlugin(id) {
    try {
      const result = await http
        .get(`/editor/plugin/detail/${id}`)
        .then(function (response) {
          // console.log(response.data.posts[0]);
          // console.log(response);
          return response.data.plugin;
        });
      // console.log(result)
      return result;
    } catch (error) {}
}

export async function createPlugin(plugin) {
  try {
    console.log(plugin)
    const result = await http
      .post("/editor/plugin/create", plugin)
      .then(function (response) {
        console.log(response,"response");
        return response.data.post;
      });
    return result;
  } catch (error) {}
}

export async function updatePlugin(plugin) {
  try {
    console.log(plugin)
    const result = await http
      .post("/editor/plugin/update", plugin)
      .then(function (response) {
        console.log(response,"response");
        return response.data.post;
      });
    return result;
  } catch (error) {}
}

export async function deletePlugin(pluginId) {
  try {
    const result = await http
      .post("/editor/plugin/delete", {plugin:pluginId})
      .then(function (response) {
        console.log(response,"response");
        return response.data.post;
      });
    return result;
  } catch (error) {}
}

export async function chat(text,pluginId=0) {
    try {
      let response = await fetch(import.meta.env.APP_BASE_URL+"/ai/chat",{
          method: 'POST',
          headers: {
              'Content-Type': 'application/json;charset=utf-8',
              'Authorization': 'Bearer ' + localStorage.getItem('token')
             },
          body: JSON.stringify({prompt: text, plugin: pluginId})
      });
      if (!response.ok) {
        if(response.status === 401){
          localStorage.removeItem('token')
          router.push('/login')
        }
        throw new Error("Network response was not ok");
      }
      const reader = response.body.getReader();
      return reader
    } catch (e) {
      console.log(e);
    }
}

export async function trans2English(text) {
  try {
    const result = await axios
      .post(
        "/ai/trans",
        { text: text },
        {
          headers: {
            "Content-Type": "multipart/form-data",
            //   'Authorization': 'Token '+'80cbfc82577e84f4835fb46fd7cec680f3992eb8',
          },
        }
      )
      .then(function (response) {
        return response;
      });
    // console.log(result)
    return result;
  } catch (error) {}
}

export async function ocrImg(imgForm) {
  try {
    
    const result = await axios.post("/ai/ocr", imgForm)
      .then(function(response){
        // console.log(response.data);
        console.log('--------ocr----------');
        return  response.data
      });
    return result
  } 
  catch (error) {
    console.log(error);
  }
}